﻿CREATE PROCEDURE s_Tracker_WaitingForNew
	@TrackerIDs varchar(max),
	@ShipmentID varchar(50) = NULL
AS

IF @ShipmentID IS NULL SET @ShipmentID = dbo.Date_Format(getdate(),5.1)

IF EXISTS(SELECT * FROM t_TrackerShipment WHERE ShipmentID = @ShipmentID)BEGIN
	RAISERROR('Shipment %s already exists.',16,1,@ShipmentID)
	RETURN
END

BEGIN TRAN

/**/DECLARE @CI int EXEC @CI = sp__Context_WorkFlow_System 1
UPDATE t_Tracker SET StatusID = dbo.MenuIDByProcID(@@PROCID) WHERE ID IN (SELECT Element FROM Split(@TrackerIDs,','))
IF @@ERROR <> 0 BEGIN ROLLBACK TRAN RETURN END
/**/EXEC sp__Context @CI

INSERT INTO t_TrackerShipment(TrackerID,ShipmentID)
SELECT Element,@ShipmentID FROM Split(@TrackerIDs,',')

IF @@ERROR <> 0 BEGIN ROLLBACK TRAN RETURN END

COMMIT TRAN




